#!/bin/bash

REPO_SERVER=192.168.20.160
TOOLS_DIR=/home/lavenliu/tools/
APP_DIR=/application/

cat > /etc/yum.repos.d/lan.repo <<EOF
[lanrepo]
name=Internal Repo
baseurl=http://192.168.20.160/x86_64
gpgcheck=0
enabled=1
EOF

yum --enablerepo=lanrepo --disablerepo=base,extras,updates,epel install -y openssl-devel pcre-devel

id nginx &> /dev/null
if [ $? -ne 0 ]; then
	echo "nginx user is not exists"
	echo "add nginx user now..."
	useradd nginx -s /sbin/nologin -M
fi

# following steps for Nginx
[ ! -d ${TOOLS_DIR} ] && mkdir -p ${TOOLS_DIR}
cd ${TOOLS_DIR}
wget http://${REPO_SERVER}/tools/nginx-1.6.3.tar.gz
tar -xf nginx-1.6.3.tar.gz
cd nginx-1.6.3
./configure --user=nginx --group=nginx --prefix=/application/nginx-1.6.3 \
--with-http_ssl_module \
--with-http_stub_status_module 
if [ $? -ne 0 ] ; then
	echo "configure failed"
	exit 1
fi

make
if [ $? -ne 0 ] ; then
	echo "make failed"
	exit 1
fi

make install
if [ $? -ne 0 ] ; then
	echo "make install failed"
	exit 1
fi

ln -s /application/nginx-1.6.3 /application/nginx

wget -O ${APP_DIR}/nginx/conf/nginx.conf http://${REPO_SERVER}/conf/nginx.conf_lb01

${APP_DIR}/nginx/sbin/nginx -t
if [ $? -ne 0 ] ; then
    echo "nginx startup failure."
fi

echo "/application/nginx/sbin/nginx" >> /etc/rc.local

rm -rf ${TOOLS_DIR}/nginx-1.6.3

